﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EvidenceManagement.Model.Security;
using EvidenceManagement.Logic;
using EvidenceManagement.Data;
using System.Security.Cryptography;
using Microsoft.Practices.Unity;

namespace EvidenceManagement.Logic.Security
{
    public class SystemUserManager : ISystemUserManager
    {
        readonly SecurityDataAccessor dataAccessor = null;
        readonly EvidenceManagementDataAccessor evidenceManagerAccessor = null;
        readonly IUnityContainer unityContainer = new UnityContainer();

        public SystemUserManager()
        {
            dataAccessor = new SecurityDataAccessor("EvidenceManagement");
            evidenceManagerAccessor = new EvidenceManagementDataAccessor("EvidenceManagement");
            
        }

        private SystemUserSecurityPolicy[] GetUserMembership(ISystemUser user)
        {
            SystemUserSecurityPolicy[] userMembership = null;

            return userMembership;
        }

        public bool VerifyPassword(ISystemUser systemUser, string enteredPassword)
        {
            StringBuilder svalue = dataAccessor.GetSystemUserSValue(systemUser.SystemUserID);
            ICrytographyManager cryptoManager = new CryptographyManager(evidenceManagerAccessor);

            HashAlgorithm algorithm = cryptoManager.GetHashAlgorithm();

            byte[] passwordBytes = Encoding.Unicode.GetBytes(enteredPassword);
            byte[] saltBytes = Convert.FromBase64String(svalue.ToString());

            List<byte> data = passwordBytes.ToList();
            data.AddRange(saltBytes);

            string hashedData = cryptoManager.GenerateHashString(algorithm, data.ToArray());

            return (string.Equals(hashedData, systemUser.Password, StringComparison.CurrentCulture));
        }

        #region ISystemUserManager Members

        public ISystemUser GetUser(string userName, string password)
        {
            SystemUser systemUser = new SystemUser() { UserName = userName.ToUpper(), Password = password };
            systemUser.SystemUserID = dataAccessor.GetSystemUserIDByUserName(userName);

            if (dataAccessor.DataObjectCommand.Read<SystemUser>(systemUser))
            {
                if (VerifyPassword(systemUser, password))
                {                  
                    dataAccessor.DataObjectCommand.ReadRelation<SystemUser>(systemUser);
                    systemUser.Password = string.Empty;
                }
                else
                    systemUser = null;

                return systemUser;
            }
            else
                return null;
        }

        public ISystemUser RegisterUser(SystemUserSecurityPolicy[] userMembership)
        {
            return null;
        }

        public bool UpdateUserDetails(ISystemUser user)
        {
            bool isUpdated = false;

            return isUpdated;
        }

        public bool UpdateUserMembership(ISystemUser user, SystemUserSecurityPolicy[] userMembership)
        {
            bool isUpdated = false;

            return isUpdated;
        }

        public bool DeleteUser(ISystemUser user)
        {
            bool isDeleted = false;

            return isDeleted;
        }

        public bool ChangePassword(ISystemUser user, string currentPassword, string newPassword)
        {
            bool hasChanged = false;

            return hasChanged;
        }

        #endregion
    }
}
